package com.cn.dao;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cn.entity.News;


/*
 * 新闻类数据操作
 */
public class NewsDao extends BaseDao {

	/**
	 * 
	 * @param news 新闻对象
	 * @return 
	 */
	 
	public int  saveNews(News news) {
		int count =0;
			con = this.getCon();
			 String sql ="insert into news values(?,?)";
			try {
				psta = con.prepareStatement(sql);
				psta.setInt(1,news.getId());
				psta.setString(2,news.getTypes());
				count = psta.executeUpdate();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				this.closeAll();
			}
			
			return count;
		}
	
	/**
	 * 查询相应新闻信息标题
	 * @return 返回所有新闻集合
	 */
	public List findListNews(String types,int pageNum) {
		List list = new ArrayList();
		con = this.getCon();
		String sql ="select * from news where types="+types+" limit " +(pageNum*10)+","+10; //每页显示10数据
		try {
			psta = con.prepareStatement(sql);
			rst = psta.executeQuery();
			while (rst.next()) {
				News news =new News();
				news.setId(rst.getInt("id"));
				news.setTitle(rst.getString("title"));
				news.setSource(rst.getString("source"));
				news.setImgmode(rst.getString("imgmode"));
				news.setImages(rst.getString("images"));
				news.setUptime(rst.getString("uptime"));
				news.setTypes(rst.getString("types"));
				news.setClick(rst.getString("click"));
				list.add(news);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.closeAll();
		}
		
		return list;
	}
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		NewsDao dt=new NewsDao();
		List listDn=dt.findListNews("0",0);
		System.out.println("数据库有:"+listDn.size());
	}

}
